package cn.wen.yinxiu.works.service.impl;

import cn.wen.yinxiu.common.toolkit.DateUtils;
import cn.wen.yinxiu.works.dao.WorksDataDao;
import cn.wen.yinxiu.works.entity.WorksDataEntity;
import cn.wen.yinxiu.works.service.WorksDataService;
import cn.wen.yinxiu.works.vo.ServeHomeInfo;
import cn.wen.yinxiu.works.vo.WorksDataSimpleVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("worksDataService")
public class WorksDataServiceImpl extends ServiceImpl<WorksDataDao, WorksDataEntity> implements WorksDataService {

    @Autowired
    private WorksDataDao worksDataDao;

    @Override
    public ServeHomeInfo getWorksDataByDay(Long userId, Integer dayNum) {
        // 获取需要最近多少天的数据
        //  1.1.播放数量 / 播放量增量
        //  1.2.主页访问量 / 访问量增量
        //  1.3.点赞数 / 点赞量增量
        //  1.4.粉丝数 / 新增粉丝数
        //  1.5.作品评论数 / 作品评论数增量
        //  1.6.视频收藏数 / 视频收藏数增量
        // 1.获取当前的之前的统计
        String currentDayStartTime = DateUtils.getToDayStartTime();
        WorksDataEntity totalWorksData = getWorksDataByCurrentDay(userId, currentDayStartTime);
        // 2.获取增量
        String preDayTime = DateUtils.decDaysOfDay(dayNum, currentDayStartTime);
        // 如果指定天数不存在说明当前的数据是本周期的全部收入
        WorksDataEntity preWorksData = getWorksDataByDayNum(userId, preDayTime);
        ServeHomeInfo target = new ServeHomeInfo();
        // 数据异常直接返回 说明用户账号异常
        if (totalWorksData == null) {
            return target;
        }
        if (totalWorksData != null && preWorksData == null) {
            // 说明前一天才刷新
            preWorksData = new WorksDataEntity();
            preWorksData.setPlayNum(0L);
            preWorksData.setWorksCommentNum(0L);
            preWorksData.setWorksCollectNum(0L);
            preWorksData.setWorksLikeNum(0L);
            preWorksData.setFansNum(0L);
            preWorksData.setHomeVisitNum(0L);
        }
        target.setPlayNum(totalWorksData.getPlayNum());
        target.setIncrPlayNum(totalWorksData.getPlayNum() - preWorksData.getPlayNum());
        target.setHomeVisitNum(totalWorksData.getHomeVisitNum());
        target.setIncrHomeVisitNum(totalWorksData.getHomeVisitNum() - preWorksData.getHomeVisitNum());
        target.setFansNum(totalWorksData.getFansNum());
        target.setIncrFansNum(totalWorksData.getFansNum() - preWorksData.getFansNum());
        target.setWorksCollectNum(totalWorksData.getWorksCollectNum());
        target.setIncrWorksCollectNum(totalWorksData.getWorksCollectNum() - preWorksData.getWorksCollectNum());
        target.setWorksLikeNum(totalWorksData.getWorksLikeNum());
        target.setIncrWorksLikeNum(totalWorksData.getWorksLikeNum() - preWorksData.getWorksLikeNum());
        target.setWorksCommentNum(totalWorksData.getWorksCommentNum());
        target.setIncrWorksCommentNum(totalWorksData.getWorksCommentNum() - preWorksData.getWorksCommentNum());
        return target;
    }

    @Override
    public boolean insertBatchWorks(List<WorksDataEntity> worksDataList) {
        return worksDataDao.insertBatch(worksDataList);
    }

    @Override
    public List<WorksDataEntity> getWorksDataByTime(String currentDayStartTime) {
        QueryWrapper<WorksDataEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("create_time", currentDayStartTime);
        return worksDataDao.selectList(queryWrapper);
    }

    @Override
    public List<WorksDataEntity> getWorksDataByNumTime(String preDayTime) {
        QueryWrapper<WorksDataEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("create_time", preDayTime);
        return worksDataDao.selectList(queryWrapper);
    }

    @Override
    public List<WorksDataSimpleVO> getWorksCoreTendency(Long userId, String filed, String preDateTime, String todayEndTime) {
        return worksDataDao.queryWorksDataByFiled(userId, filed, preDateTime, todayEndTime);
    }

    @Override
    public WorksDataEntity queryWorksDataByTime(String time, Long userId) {
        QueryWrapper<WorksDataEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("create_time", time);
        WorksDataEntity dataEntity = worksDataDao.selectOne(queryWrapper);
        return dataEntity;
    }

    private WorksDataEntity getWorksDataByCurrentDay(Long userId, String currentDayStartTime) {
        QueryWrapper<WorksDataEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("create_time", currentDayStartTime);
        WorksDataEntity dataEntity = worksDataDao.selectOne(queryWrapper);
        return dataEntity;
    }

    private WorksDataEntity getWorksDataByDayNum(Long userId, String preDayTime) {
        QueryWrapper<WorksDataEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("create_time", preDayTime);
        WorksDataEntity dataEntity = worksDataDao.selectOne(queryWrapper);
        return dataEntity;
    }

    public static void main(String[] args) {
        String currentDayStartTime = DateUtils.getToDayStartTime();
        System.out.println(currentDayStartTime);
    }
}